package com.snail.utils;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PayUtil1 {
//    @Autowired
//    private OrderService orderService;
    //appid
    private final String APP_ID = "9021000151612912";
    //应用私钥
    private final String APP_PRIVATE_KEY = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggS" +
            "lAgEAAoIBAQCSnbBdfJ51MAM1WY7kad5ktmcb/dSenzmfwJ2EJHNl/7cBQm+QRzwYWxkXORUx" +
            "v1NuIVCHVQ4ZR7PCt09kDeLP2PE7VRLClVEFiJDU33ecWR3gp+u9KXU0q6nHlIVt3hjVIDtV0N" +
            "SqQXJhFRiCdgdAcwTraHri00+aVQYSK0j5SX2rAyTgEzpQSUpIoOnB00yFkXgiJxDYGt+ROP" +
            "IecLqzEspy+jCu1GI/NZWsfVL6mi8Dqj7iJuFUaUiyKckjMWsAXK3SStSWqx5r8FGtwtKF4" +
            "/ZuCoTv49c74ov1AYsvrQnlNSZo95CJ3+uXU+qdgd/9OSqk5+rhizNeGWODKFU9AgMBAAECgg" +
            "EBAISVFGd1ydUvFbeoxQ7IrdNlHtixMRQ/6FwOUmKsk64pD9STv085G57qwkW92Oyjl05/0dK" +
            "Pl7BEpsnt+l0AMZrDE2IUDGLpo9rdJBtb+U3pSJtfPk5QryxyD5XrOL2hmdMg0odH0QglJsmjyg" +
            "YvtV8So+A9GuMDM3jcaiEEA3lpGzhJ1DJhXYov3Kwv4Hs/KuluNPsFqTT9NkiSaobvClrvcM41" +
            "oAjPRm8bWe80QwDA105G1yzkYPMP31Q6kAIYaoW+FOC2wwL1tWSljBfXvlCucO2m6iiGZ1kQj2" +
            "ZqYvc6H5CgNj0uswE0tSnKZUwXjXQ4ENI3z/cTGc7GIXY2AgECgYEA2EGyPJnRbYKAaTxsByE" +
            "E4cIWXayx3j5hvK47xybBaZwLd4B7y55F2UTV1GFPf7IgzKAew+bnvmm6wSivDLSMrMLPhU+L" +
            "mrCAteoLsWdW+bokOylQmwtB3h3z20V3AQA8rgVOaigJ4OALgZ9KJQ542FitwucxEmIoz0mAQ" +
            "Zh/JS8CgYEArY+VajncjDN2mm4EuhQ6n6sLSHL+Kg2rZyWT5KNBNjr+s/596ydMZVJrDP9eQ9" +
            "lSTMKEIXZKIon8sAU4gY+k06yDFzzAuwgzkHrdgQvvEZJULrTdeU7b9fCg1KoJ8kPFaNJbGC" +
            "/nrCOTfOmqDVc0UsnXr5Ng/v/JKQxxZAGBaVMCgYEA1XOq6NPuegfYlYbVHghCWE8JYnalrba" +
            "nI+RaDo07VPF4BAEmr+lOClE+Of2+yUzZwP++viB412JmumdcuM6xF4jF6lCEfDABKw2TcwyM" +
            "Nbu8LUTvUcm/vuyTmPesqalBO4bzSiaw8WxTDyNZvM/XCbwl7n3h2sJWg5uJ47UQsnMCgYEAgG" +
            "peWOxa3HkvZe/FG5DgF0dM39wFZkQa8ASDuoV4OJHJ5o+DxzuacNVcU4+WxYjENfsuHDwTeh6yk" +
            "eIq/uGzSNK5Y7YVu8zDqkDeJx8XUd783loADENmyD+0H+V32oJ+S6JouCqptVnVRHT210wQc" +
            "LjbFxNqjwHearrEIwRiJfUCgYAXFNEkdi9mfPUVXXAPoyAmLdbUa0PQQDtQ/coQmzMcfNG5J" +
            "CnnsZRncaJJnHDwwUUy7zEh5EJggePNRIunExpX7X1TavcSP6SE9ZxqkSuqz91Dlu9Yizvlji" +
            "fTjRT16eFgTJWUtEuAoJxah8wbPHAyNBJxXwEWQM2af/xRl6aDWQ==";

    private final String CHARSET = "UTF-8";
    // 支付宝公钥
    private final String ALIPAY_PUBLIC_KEY ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAobYDKnEhHjfG5ajt84L9qiz0jTjcKn/GxiUGh3Xd0cwRlxHObqonumIhwUBAXX9CjSrbgzW3Ox8LRl38nTuvtN+HH4XvyzBljEiHdU/HcedEjnk8B0yiWdLMp4KfIPZq9XW0V+H4NNq1hYu7QDLMoTwNglEgxyazISlgWUfeqYq1EQutqmN2rQDbkVxSoflTTTu1sF8fWNRVryluyM+bzEsFqjHd19HZfil4OrFWXv/1UuioqYodjkDftBUpguI0uFFKSLIuJy1HYIEzvXu0dNERAc+oPydlV/Z901SfzvUooZehy5qv5F6MfOR+UNInCtISWbuo4+f2Q8kehoTnrwIDAQAB";

    private final String GATEWAY_URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = " http://k349dc68.natappfree.cc/api/alipay/toSuccess";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private final String RETURN_URL = "http://localhost:8080/api/alipay/toSuccess";
    private AlipayClient alipayClient = null;
    //支付宝官方提供的接口
    public String sendRequestToAlipay(String outTradeNo, Float totalAmount, String subject) throws AlipayApiException {
        //获得初始化的AlipayClient
        alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(RETURN_URL);
        alipayRequest.setNotifyUrl(NOTIFY_URL);

        //商品描述（可空）
        String body = "";
        alipayRequest.setBizContent("{\"out_trade_nos\":\"" + outTradeNo + "\","
                + "\"total_amount\":\"" + totalAmount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println("返回的结果是："+result );
        return result;
    }

    //    通过订单编号查询
    public String query(String id){
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", id);
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = null;
        String body=null;
        try {
            response = alipayClient.execute(request);
            body = response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return body;
    }
}
